package LeetCode.leetcode216;


import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {

        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backTracking(path,res,0,k,n,1);
        return res;
    }

    public static void backTracking(List<Integer> path, List<List<Integer>> res, int sum, int k, int n,int startIndex) {
        if (path.size() == k){
            if (sum == n) {
                res.add(new ArrayList<>(path));
            }
            return;
        }
        for (int i = startIndex; i <= 9; i++) {
            path.add(i);
            sum+=i;
            backTracking(path,res,sum,k,n,i+1);
            path.remove(path.size()-1);
            sum-=i;
        }
    }
}
